<template>
  <div style="width:100%;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;">
    <el-menu
        class="el-menu-demo"
        :default-active="activeIndex2"
        mode="horizontal"
        @select="handleSelect"
        router
    >
      <img class="logo" rel="icon" src="../../../../public/vite.svg" />
      <span class="title">安颐智护系统</span>
      <div style="width:100px;"></div>
      <MenuItem />
      <Dropdown />
    </el-menu>

    <el-card shadow="hover" class="menu-demo">
      <el-row :gutter="20">
        <el-col :span="3">
          <div class="grid-content ep-bg-purple">
            <el-page-header :icon="ArrowLeft" style="position: relative; bottom: 15px;" @back="goBack">
              <template #content>
                <el-breadcrumb :separator-icon="ArrowRight" style="white-space: nowrap;">
                  <el-breadcrumb-item :to="{ path: '/homePage' }">
                    <span style="font-weight: bolder; font-size: 16px;">主页</span>
                  </el-breadcrumb-item>
                </el-breadcrumb>
              </template>
            </el-page-header>
          </div>
        </el-col>
        <el-col :span="18">
          <div class="grid-content ep-bg-purple">
            <el-tabs
                v-model="editableTabsValue"
                type="card"
                :closable="isClos"
                class="demo-tabs"
                @edit="handleTabsEdit"
                @tab-change="tabChange"
                style="position: relative;bottom:22px;"
            >
              <el-tab-pane
                  v-for="(item,index) in routeStore().globalRoute"
                  :key="index"
                  :label="(item.title as string)"
                  :name="(item.path as string)"
              >
              </el-tab-pane>
            </el-tabs>
          </div>
        </el-col>
        <el-col :span="3">
          <div class="grid-content ep-bg-purple">
            <el-tag class="tag" el-check-tag checked>当前页面：{{ nowPageStore().nowPage }}</el-tag>
          </div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>


<script lang="ts" setup>

import { ref,onMounted } from 'vue'
import MenuItem from '../menu-item/caregiver-item.vue'
import Dropdown from '../dropdown.vue'
import { ArrowRight } from '@element-plus/icons-vue'
import { ArrowLeft } from '@element-plus/icons-vue'
import { routeStore,nowPageStore} from '@/store/modules/routesInfo'
import router from '@/router'
import type { TabPaneName,TabsPaneContext } from 'element-plus'
//按钮关闭逻辑包
import { recognize_close_click,invade_close_click, monitor_close_click, register_close_click, track_close_click } from "@/api/close";



onMounted(()=>{
  console.log('1',routeStore().globalRoute)
  editableTabsValue.value=routeStore().globalRoute[routeStore().globalRoute.length-1].path as string
})
const activeIndex2 = ref('/homePage')
const handleSelect = (key: string, keyPath: string[]) => {
  editableTabsValue.value=key
}
const goBack = () => {
  close()
  router.go(-1)
}
let isClos = ref<boolean>(true);
const editableTabsValue = ref<string>()


const handleTabsEdit = (
    targetName: TabPaneName | undefined,
    action: 'remove' | 'add'
) => {
  if (action === 'remove') {

    close()

    const tabs = routeStore().globalRoute
    let activeName = editableTabsValue.value
    if (activeName === targetName) {
      let newIndex = -1;
      tabs.forEach((tab, index) => {
        if (tab.path === targetName) {
          newIndex = index;
        }
      })//forEach
      let nextTab = tabs[newIndex + 1]; // Try to find the next tab
      for (let i = newIndex + 1; i < tabs.length && !nextTab; i++) {
        nextTab = tabs[i];
      }
      if (!nextTab) { // If there is no next tab, find the previous tab
        for (let i = newIndex - 1; i >= 0 && !nextTab; i--) {
          nextTab = tabs[i];
        }
      }
      if (nextTab) {
        activeName = nextTab.path as string
      }
    }//if
    editableTabsValue.value = activeName
    routeStore().globalRoute = tabs.filter((tab) => tab.path !== targetName)

    if (routeStore().globalRoute.length === 0) {
      // close()
      router.push('/homePage') // if all tabs are closed, go to homepage
    } else if (activeName) {
      // close()
      router.push(activeName) // add this line to change the route when a tab is closed
    }
  }//if-action
}//handleTabsEdit
const tabChange = (name: TabPaneName) =>{
  console.log('tabChange')
  router.push(name as string);
}
function close(){

  register_close_click().then((response) => {

    //   var res = response.data
    //   if (res.respCode === '000000') {
    //     //ElMessage.success('关闭成功');
    //
    //   } else {
    //     ElMessage.error(res.respMsg);
    //   }
    // })
    //     .catch((error) => {
    //       console.error(error);
    //       // ElMessage.error('关闭失败，请重试');
  });
  invade_close_click().then((response) => {

    //   var res = response.data
    //   if (res.respCode === '000000') {
    //     //ElMessage.success('关闭成功');
    //
    //   } else {
    //     ElMessage.error(res.respMsg);
    //   }
    // })
    //     .catch((error) => {
    //       console.error(error);
    //       // ElMessage.error('关闭失败，请重试');
  });
  track_close_click().then((response) => {

    //   var res = response.data
    //   if (res.respCode === '000000') {
    //     //ElMessage.success('关闭成功');
    //
    //   } else {
    //     ElMessage.error(res.respMsg);
    //   }
    // })
    //     .catch((error) => {
    //       console.error(error);
    //       // ElMessage.error('关闭失败，请重试');
  });
  recognize_close_click().then((response) => {

    //   var res = response.data
    //   if (res.respCode === '000000') {
    //     //ElMessage.success('关闭成功');
    //
    //   } else {
    //     ElMessage.error(res.respMsg);
    //   }
    // })
    //     .catch((error) => {
    //       console.error(error);
    //       // ElMessage.error('关闭失败，请重试');
  });
  monitor_close_click().then((response) => {

    //   var res = response.data
    //   if (res.respCode === '000000') {
    //     //ElMessage.success('关闭成功');
    //
    //   } else {
    //     ElMessage.error(res.respMsg);
    //   }
    // })
    //     .catch((error) => {
    //       console.error(error);
    //       // ElMessage.error('关闭失败，请重试');
  });


}

</script>

<style scoped>

.el-menu-demo {
  width: 100%;
  height: 60px;
}

.menu-demo {
  width: 100%;
  height: 40px;
}

.logo{
  margin-left: 30px;
  margin-right: 10px;
  margin-top: 10px;
  width: 40px;
  height: 40px;
}

.title{
  font-size: 24px;
  font-weight: bolder;
  margin-top: 15px;
  color: #030303;
}

.tag{
  position: relative;
  bottom: 21px;
  float: right;
  width: 250px;
  height: 40px;
  font-weight: bolder;
  font-size: 20px;
  margin-right: 0px;
}

</style>